#################################################################
# "Democracy, Inequality and Antitrust"                         #
#   By Michael O Allen, Kenneth Scheve, and David Stasavage     #
#                                                               #
#   Last modified: 12/22/2024                                   #
#################################################################

library(data.table)
library(cowplot)
library(ggplot2)

library(here)

# Figure 1a ---------------------------------------------------------------

# Must download the "Comparative Competition Law Index (“CLI”)" dataset.
# Available here: https://comparativecompetitionlaw.org/data/
cli <- haven::read_dta(here('data', 'CCL_CLI_Data_Ver2.dta'))
setDT(cli)

ggplot(cli[, .(newLaws = sum(competition_law_coded_total)), by = year],
       aes(x = year, y = newLaws)) +
  geom_col(fill = "#007fcf",
           color = "#007fcf",
           width = .4) +
  scale_y_continuous(expand = expansion(mult = c(0, .01)),
                     breaks = seq(5, 35, 5)) +
  scale_x_continuous(breaks = seq(1890, 2010, 20)) +
  labs(x = '', y = 'Mean CLI') +
  theme_cowplot() +
  background_grid(major = 'y',
                  minor = 'y') +
  theme(legend.position = 'none')

ggsave(here('output', 'figures', 'figure-1a.png'),
       height = 4, width = 6, units = 'in', dpi = 450)

rm(cli)

# Figure 1b ---------------------------------------------------------------

df_yearly <- fread(here('data', 'cli_yearly.csv'))

df_yearly$dem_bin <- factor(df_yearly$dem_bin, levels = 1:0)

plotData_cli <- df_yearly[!is.na(dem_bin), .(cli_mean = mean(cli_overall_norm, na.rm = TRUE)),
                          keyby = .(year, dem_bin)]

ggplot(plotData_cli,
       aes(x = year, y = cli_mean, color = factor(dem_bin))) +
  geom_line(linewidth = .75) +
  annotate('text', x = 1956, y = .34, hjust = 0, size = 4.25, color = '#007fcf',
           label = 'Democracies') +
  annotate('text', x = 1956, y = .12, hjust = 0, size = 4.25, color = 'tomato2',
           label = 'Non-democracies') +
  scale_color_manual(values = c('#007fcf', 'tomato2')) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1)),
                     breaks = seq(.1, .6, .1)) +
  scale_x_continuous(breaks = seq(1890, 2010, 20)) +
  labs(x = '', y = 'Mean CLI') +
  theme_cowplot() +
  background_grid(major = 'y',
                  minor = 'y') +
  theme(legend.position = 'none')

ggsave(here('output', 'figures', 'figure-1b.png'),
       height = 4, width = 6, units = 'in', dpi = 450)
